#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<unistd.h>
#include<sys/types.h>
#include<sys/wait.h>
#define SIZE 1024

char* argv[SIZE];
int lastcode=0;
int Interactive(char out[],int size)
{
    printf("[%s@%s %s]$ ",getenv("USER"),getenv("HOSTNAME"),getenv("PWD"));
    fgets(out,size,stdin);
    out[strlen(out)-1]=0;
    return strlen(out);
}

void SplitStr(char str[])
{
    argv[0]=strtok(str," ");
    int i=1;
    while(argv[i++]=strtok(NULL," "));
}

void Execute()
{
    pid_t pid=fork();
    if(pid==0)
    {
        execvp(argv[0],argv);
        exit(1);
    }
 int status = 0;
    pid_t rid = waitpid(pid, &status, 0);
    if(rid ==pid) lastcode = WEXITSTATUS(status); 
}
int main()
{
    while(1)
    {
        char CommandStr[SIZE];
        //打印命令行提示符，并获取命令行字符串
        int n=Interactive(CommandStr,SIZE);
        if(n==0) 
        {
            continue;
        }
        //切割字符串
        SplitStr(CommandStr);
        //for(int i=0;argv[i];i++)
        //{
           // printf("argv[%d]: %s\n ",i,argv[i]);
       //bin }
        //执行命令
        Execute();
    }
    return 0;
}
